package com.zhaodui.order.service.impl;
import com.zhaodui.order.service.FxjOrderMainServiceI;
import com.zhaodui.util.fxjUtil;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.order.entity.FxjOrderMainEntity;
import com.zhaodui.order.page.FxjOrderMainPage;
import com.zhaodui.order.entity.FxjOrderLogEntity;
import com.zhaodui.order.entity.FxjOrderObjEntity;
import com.zhaodui.order.entity.FxjOrderLoanEntity;
import com.zhaodui.order.entity.FxjOrderEmerEntity;
import com.zhaodui.order.entity.FxjOrderDueEntity;
import com.zhaodui.order.entity.FxjOrderAttaEntity;
import com.zhaodui.order.entity.FxjOrderRmEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;

import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

import static com.xiaoleilu.hutool.date.DateTime.now;
import static com.zhaodui.util.ConStant.*;

@Service("fxjOrderMainService")
@Transactional
public class FxjOrderMainServiceImpl extends CommonServiceImpl implements FxjOrderMainServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
 	public void delete(FxjOrderMainEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(FxjOrderMainPage fxjOrderMainPage) throws Exception {
		FxjOrderMainEntity fxjOrderMain = new FxjOrderMainEntity();
		fxjOrderMainPage.setCreateDate(now());
		MyBeanUtils.copyBeanNotNull2Bean(fxjOrderMainPage, fxjOrderMain);
		//保存主信息
		this.save(fxjOrderMain);
		/**保存-日志*/
		List<FxjOrderLogEntity> fxjOrderLogList = fxjOrderMainPage.getFxjOrderLogList();
		for(FxjOrderLogEntity fxjOrderLog:fxjOrderLogList){
			//外键设置
			fxjOrderLog.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderLog);
		}
		/**保存-产品信息*/
		List<FxjOrderObjEntity> fxjOrderObjList = fxjOrderMainPage.getFxjOrderObjList();
		for(FxjOrderObjEntity fxjOrderObj:fxjOrderObjList){
			//外键设置
			fxjOrderObj.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderObj);
		}
		/**保存-融资物*/
		List<FxjOrderLoanEntity> fxjOrderLoanList = fxjOrderMainPage.getFxjOrderLoanList();
		for(FxjOrderLoanEntity fxjOrderLoan:fxjOrderLoanList){
			//外键设置
			fxjOrderLoan.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderLoan);
		}
		/**保存-紧急联系人*/
		List<FxjOrderEmerEntity> fxjOrderEmerList = fxjOrderMainPage.getFxjOrderEmerList();
		for(FxjOrderEmerEntity fxjOrderEmer:fxjOrderEmerList){
			//外键设置
			fxjOrderEmer.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderEmer);
		}
		/**保存-收还款*/
		List<FxjOrderDueEntity> fxjOrderDueList = fxjOrderMainPage.getFxjOrderDueList();
		for(FxjOrderDueEntity fxjOrderDue:fxjOrderDueList){
			//外键设置
			fxjOrderDue.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderDue);
		}
		/**保存-附件*/
		List<FxjOrderAttaEntity> fxjOrderAttaList = fxjOrderMainPage.getFxjOrderAttaList();
		for(FxjOrderAttaEntity fxjOrderAtta:fxjOrderAttaList){
			//外键设置
			fxjOrderAtta.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderAtta);
		}
		/**保存-风控*/
		List<FxjOrderRmEntity> fxjOrderRmList = fxjOrderMainPage.getFxjOrderRmList();
		for(FxjOrderRmEntity fxjOrderRm:fxjOrderRmList){
			//外键设置
			fxjOrderRm.setOrderNo(fxjOrderMain.getOrderNo());
			this.save(fxjOrderRm);
		}
	}

	
	public void updateMain(FxjOrderMainPage fxjOrderMainPage) throws Exception{
		FxjOrderMainEntity fxjOrderMain = new FxjOrderMainEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(fxjOrderMainPage.getId())){
			fxjOrderMain = findUniqueByProperty(FxjOrderMainEntity.class, "id", fxjOrderMainPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(fxjOrderMainPage, fxjOrderMain);

			if (fxjOrderMain.getBpmStatus().equals(bpms20)){//如果是已审批   则生成抵押品管理
				fxjUtil.genLoanMa(fxjOrderMain.getOrderNo());
			}

			if (fxjOrderMain.getBpmStatus().equals(bpms30)){//如果是已过户   则生成保险指派
				fxjUtil.genLoanIns(fxjOrderMain.getOrderNo());
			}

			if (fxjOrderMain.getBpmStatus().equals(bpms70)){//如果是已提交银行   则生成放款列表
				fxjUtil.genLoanFK(fxjOrderMain.getOrderNo());
			}
			if (fxjOrderMain.getBpmStatus().equals(bpms80)){//如果是已放款   则生成还款计划表
				fxjUtil.genRePlan(fxjOrderMain.getOrderNo());
			}
			this.saveOrUpdate(fxjOrderMain);
		}else{
			this.saveOrUpdate(fxjOrderMain);
		}
		//===================================================================================
		//获取参数
		Object orderNo0 = fxjOrderMain.getOrderNo();
		Object orderNo1 = fxjOrderMain.getOrderNo();
		Object orderNo2 = fxjOrderMain.getOrderNo();
		Object orderNo3 = fxjOrderMain.getOrderNo();
		Object orderNo4 = fxjOrderMain.getOrderNo();
		Object orderNo5 = fxjOrderMain.getOrderNo();
		Object orderNo6 = fxjOrderMain.getOrderNo();
		//===================================================================================
		//1.日志数据库的数据
	    String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderLogEntity> fxjOrderLogOldList = this.findHql(hql0,orderNo0);
		//2.日志新的数据
		List<FxjOrderLogEntity> fxjOrderLogList = fxjOrderMainPage.getFxjOrderLogList();
	    //3.筛选更新明细数据-日志
		if(fxjOrderLogList!=null &&fxjOrderLogList.size()>0){
			for(FxjOrderLogEntity oldE:fxjOrderLogOldList){
				boolean isUpdate = false;
				for(FxjOrderLogEntity sendE:fxjOrderLogList){
					//需要更新的明细数据-日志
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-日志
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-日志
			for(FxjOrderLogEntity fxjOrderLog:fxjOrderLogList){
				if(oConvertUtils.isEmpty(fxjOrderLog.getId())){
					//外键设置
					fxjOrderLog.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderLog);
				}
			}
		}
		//===================================================================================
		//1.产品信息数据库的数据
	    String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderObjEntity> fxjOrderObjOldList = this.findHql(hql1,orderNo1);
		//2.产品信息新的数据
		List<FxjOrderObjEntity> fxjOrderObjList = fxjOrderMainPage.getFxjOrderObjList();
	    //3.筛选更新明细数据-产品信息
		if(fxjOrderObjList!=null &&fxjOrderObjList.size()>0){
			for(FxjOrderObjEntity oldE:fxjOrderObjOldList){
				boolean isUpdate = false;
				for(FxjOrderObjEntity sendE:fxjOrderObjList){
					//需要更新的明细数据-产品信息
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-产品信息
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-产品信息
			for(FxjOrderObjEntity fxjOrderObj:fxjOrderObjList){
				if(oConvertUtils.isEmpty(fxjOrderObj.getId())){
					//外键设置
					fxjOrderObj.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderObj);
				}
			}
		}
		//===================================================================================
		//1.融资物数据库的数据
	    String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderLoanEntity> fxjOrderLoanOldList = this.findHql(hql2,orderNo2);
		//2.融资物新的数据
		List<FxjOrderLoanEntity> fxjOrderLoanList = fxjOrderMainPage.getFxjOrderLoanList();
	    //3.筛选更新明细数据-融资物
		if(fxjOrderLoanList!=null &&fxjOrderLoanList.size()>0){
			for(FxjOrderLoanEntity oldE:fxjOrderLoanOldList){
				boolean isUpdate = false;
				for(FxjOrderLoanEntity sendE:fxjOrderLoanList){
					//需要更新的明细数据-融资物
					if(oldE.getElementNo().equals(sendE.getElementNo())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-融资物
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-融资物
			for(FxjOrderLoanEntity fxjOrderLoan:fxjOrderLoanList){
				if(oConvertUtils.isEmpty(fxjOrderLoan.getId())){
					//外键设置
					fxjOrderLoan.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderLoan);
				}
			}
		}
		//===================================================================================
		//1.紧急联系人数据库的数据
	    String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderEmerEntity> fxjOrderEmerOldList = this.findHql(hql3,orderNo3);
		//2.紧急联系人新的数据
		List<FxjOrderEmerEntity> fxjOrderEmerList = fxjOrderMainPage.getFxjOrderEmerList();
	    //3.筛选更新明细数据-紧急联系人
		if(fxjOrderEmerList!=null &&fxjOrderEmerList.size()>0){
			for(FxjOrderEmerEntity oldE:fxjOrderEmerOldList){
				boolean isUpdate = false;
				for(FxjOrderEmerEntity sendE:fxjOrderEmerList){
					//需要更新的明细数据-紧急联系人
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-紧急联系人
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-紧急联系人
			for(FxjOrderEmerEntity fxjOrderEmer:fxjOrderEmerList){
				if(oConvertUtils.isEmpty(fxjOrderEmer.getId())){
					//外键设置
					fxjOrderEmer.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderEmer);
				}
			}
		}
		//===================================================================================
		//1.收还款数据库的数据
	    String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderDueEntity> fxjOrderDueOldList = this.findHql(hql4,orderNo4);
		//2.收还款新的数据
		List<FxjOrderDueEntity> fxjOrderDueList = fxjOrderMainPage.getFxjOrderDueList();
	    //3.筛选更新明细数据-收还款
		if(fxjOrderDueList!=null &&fxjOrderDueList.size()>0){
			for(FxjOrderDueEntity oldE:fxjOrderDueOldList){
				boolean isUpdate = false;
				for(FxjOrderDueEntity sendE:fxjOrderDueList){
					//需要更新的明细数据-收还款
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-收还款
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-收还款
			for(FxjOrderDueEntity fxjOrderDue:fxjOrderDueList){
				if(oConvertUtils.isEmpty(fxjOrderDue.getId())){
					//外键设置
					fxjOrderDue.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderDue);
				}
			}
		}
		//===================================================================================
		//1.附件数据库的数据
	    String hql5 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderAttaEntity> fxjOrderAttaOldList = this.findHql(hql5,orderNo5);
		//2.附件新的数据
		List<FxjOrderAttaEntity> fxjOrderAttaList = fxjOrderMainPage.getFxjOrderAttaList();
	    //3.筛选更新明细数据-附件
		if(fxjOrderAttaList!=null &&fxjOrderAttaList.size()>0){
			for(FxjOrderAttaEntity oldE:fxjOrderAttaOldList){
				boolean isUpdate = false;
				for(FxjOrderAttaEntity sendE:fxjOrderAttaList){
					//需要更新的明细数据-附件
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-附件
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-附件
			for(FxjOrderAttaEntity fxjOrderAtta:fxjOrderAttaList){
				if(oConvertUtils.isEmpty(fxjOrderAtta.getId())){
					//外键设置
					fxjOrderAtta.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderAtta);
				}
			}
		}
		//===================================================================================
		//1.风控数据库的数据
	    String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderRmEntity> fxjOrderRmOldList = this.findHql(hql6,orderNo6);
		//2.风控新的数据
		List<FxjOrderRmEntity> fxjOrderRmList = fxjOrderMainPage.getFxjOrderRmList();
	    //3.筛选更新明细数据-风控
		if(fxjOrderRmList!=null &&fxjOrderRmList.size()>0){
			for(FxjOrderRmEntity oldE:fxjOrderRmOldList){
				boolean isUpdate = false;
				for(FxjOrderRmEntity sendE:fxjOrderRmList){
					//需要更新的明细数据-风控
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-风控
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-风控
			for(FxjOrderRmEntity fxjOrderRm:fxjOrderRmList){
				if(oConvertUtils.isEmpty(fxjOrderRm.getId())){
					//外键设置
					fxjOrderRm.setOrderNo(fxjOrderMain.getOrderNo());
					this.save(fxjOrderRm);
				}
			}
		}
	}

	public void delMain(FxjOrderMainEntity fxjOrderMain) throws Exception{
		//删除主表信息
		this.delete(fxjOrderMain);
		//===================================================================================
		//获取参数
		Object orderNo0 = fxjOrderMain.getOrderNo();
		Object orderNo1 = fxjOrderMain.getOrderNo();
		Object orderNo2 = fxjOrderMain.getOrderNo();
		Object orderNo3 = fxjOrderMain.getOrderNo();
		Object orderNo4 = fxjOrderMain.getOrderNo();
		Object orderNo5 = fxjOrderMain.getOrderNo();
		Object orderNo6 = fxjOrderMain.getOrderNo();
		//===================================================================================
		//删除-日志
	    String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderLogEntity> fxjOrderLogOldList = this.findHql(hql0,orderNo0);
		this.deleteAllEntitie(fxjOrderLogOldList);
		//===================================================================================
		//删除-产品信息
	    String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderObjEntity> fxjOrderObjOldList = this.findHql(hql1,orderNo1);
		this.deleteAllEntitie(fxjOrderObjOldList);
		//===================================================================================
		//删除-融资物
	    String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderLoanEntity> fxjOrderLoanOldList = this.findHql(hql2,orderNo2);
		this.deleteAllEntitie(fxjOrderLoanOldList);
		//===================================================================================
		//删除-紧急联系人
	    String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderEmerEntity> fxjOrderEmerOldList = this.findHql(hql3,orderNo3);
		this.deleteAllEntitie(fxjOrderEmerOldList);
		//===================================================================================
		//删除-收还款
	    String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderDueEntity> fxjOrderDueOldList = this.findHql(hql4,orderNo4);
		this.deleteAllEntitie(fxjOrderDueOldList);
		//===================================================================================
		//删除-附件
	    String hql5 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderAttaEntity> fxjOrderAttaOldList = this.findHql(hql5,orderNo5);
		this.deleteAllEntitie(fxjOrderAttaOldList);
		//===================================================================================
		//删除-风控
	    String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjOrderRmEntity> fxjOrderRmOldList = this.findHql(hql6,orderNo6);
		this.deleteAllEntitie(fxjOrderRmOldList);
	}
 	
}